package com.ywh.composetest

import android.os.Parcelable
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import kotlinx.parcelize.RawValue

/**
 * 1.可以注解data class 和class
 * 构造函数的变量能序列化，name和age
 * 变量address无法序列化，phone是常量
 *
 * 2.可以注解sealed class ,子类无需再注解
 *
 * 3.@IgnoredOnParcel修饰的变量构造函数内的var、val,必须要有默认值，否则编译报错，注解后该字段不会序列化
 */
@Parcelize
data class DataClass(var name: String, val age: Int) : Parcelable {
    var address: String = ""
    val phone: String = "150xxx"

    override fun toString(): String {
        return "name:${name},age:${age}" + ",address:${address},phone:${phone}"
    }
}

/**
 * 构造函数的变量能序列化，name和age
 * 变量address无法序列化，phone是常量
 */
@Parcelize
class MyClass(var name: String, val age: Int) : Parcelable {
    var address: String = ""
    val phone: String = "150xxx"

    override fun toString(): String {
        return "name:${name},age:${age}" + ",address:${address},phone:${phone}"
    }
}

@Parcelize
sealed class SealedClass : Parcelable {
    class A(val name1: String) : SealedClass()
    class B(var age: Int) : SealedClass()
    data object C : SealedClass()
}


@Parcelize
data class DataClass2(
    @IgnoredOnParcel var name: String = "defaultName",
    @IgnoredOnParcel val age: Int = 1
) : Parcelable {
    var address: String = ""

    val phone: String = "150xxx"

    override fun toString(): String {
        return "name:${name},age:${age}" + ",address:${address},phone:${phone}"
    }
}

@Parcelize
class MyClass2(val name: String = "defaultName", val age: Int) : Parcelable {
    var address: String = ""
    val phone: String = "150xxx"

    override fun toString(): String {
        return "name:${name},age:${age}" + ",address:${address},phone:${phone}"
    }
}


//暂时没找到使用场景
// @RawValue不进行序列化和反序列化
// 必须是序列化的对象
//比如bitmap不需要进行序列化和反序列化。
@Parcelize
class MyClass3(val external: @RawValue ExternalClass) : Parcelable

@Parcelize
class ExternalClass(val name: String, val age: Int) : Parcelable {
    override fun toString(): String {
        return "name:${name},age:${age}"
    }
}